
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>staticfiles 应用 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="Feed 聚合框架" href="syndication.html" />
    <link rel="prev" title="“站点”框架" href="sites.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);(function($) {
    $(document).ready(function() {
        $(".c-tab-unix").on("click", function() {
            $("section.c-content-unix").show();
            $("section.c-content-win").hide();
            $(".c-tab-unix").prop("checked", true);
        });
        $(".c-tab-win").on("click", function() {
            $("section.c-content-win").show();
            $("section.c-content-unix").hide();
            $(".c-tab-win").prop("checked", true);
        });
    });
})(jQuery);</script>
<link rel="stylesheet" href="../../_static/console-tabs.css" type="text/css" />
  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="sites.html" title="“站点”框架">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="syndication.html" title="Feed 聚合框架">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-staticfiles">
            
  <div class="section" id="s-module-django.contrib.staticfiles">
<span id="s-the-staticfiles-app"></span><span id="module-django.contrib.staticfiles"></span><span id="the-staticfiles-app"></span><h1><code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 应用<a class="headerlink" href="#module-django.contrib.staticfiles" title="永久链接至标题">¶</a></h1>
<p><code class="docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code> 从你的每一个应用程序（以及你指定的任何其他地方）收集静态文件到一个单一的位置，可以很容易地在生产中服务。</p>
<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<p class="last">关于静态文件应用的介绍和一些使用示例，请参见 <a class="reference internal" href="../../howto/static-files/index.html"><span class="doc">管理静态文件（比如图片、JavaScript、CSS）</span></a>。关于部署静态文件的指南，请参见 <a class="reference internal" href="../../howto/static-files/deployment.html"><span class="doc">部署静态文件</span></a>。</p>
</div>
<div class="section" id="s-settings">
<span id="s-staticfiles-settings"></span><span id="settings"></span><span id="staticfiles-settings"></span><h2>配置<a class="headerlink" href="#settings" title="永久链接至标题">¶</a></h2>
<p>关于以下配置，请参见 <a class="reference internal" href="../settings.html#settings-staticfiles"><span class="std std-ref">静态文件配置</span></a>：</p>
<ul class="simple">
<li><a class="reference internal" href="../settings.html#std:setting-STATIC_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code></a></li>
<li><a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a></li>
<li><a class="reference internal" href="../settings.html#std:setting-STATICFILES_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_DIRS</span></code></a></li>
<li><a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a></li>
<li><a class="reference internal" href="../settings.html#std:setting-STATICFILES_FINDERS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_FINDERS</span></code></a></li>
</ul>
</div>
<div class="section" id="s-management-commands">
<span id="management-commands"></span><h2>管理命令<a class="headerlink" href="#management-commands" title="永久链接至标题">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code> 公开了三个管理命令。</p>
<div class="section" id="s-collectstatic">
<span id="collectstatic"></span><h3><code class="docutils literal notranslate"><span class="pre">collectstatic</span></code><a class="headerlink" href="#collectstatic" title="永久链接至标题">¶</a></h3>
<dl class="django-admin">
<dt id="django-admin-collectstatic">
<code class="descname">django-admin collectstatic</code><a class="headerlink" href="#django-admin-collectstatic" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>将静态文件收集到 <a class="reference internal" href="../settings.html#std:setting-STATIC_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code></a> 中。</p>
<p>重复的文件名默认的解析方式与模板解析的方式类似：首先在指定位置找到的文件将被使用。如果你感到困惑， <a class="reference internal" href="#django-admin-findstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">findstatic</span></code></a> 命令可以帮助你显示哪些文件被找到。</p>
<p>在随后运行 <code class="docutils literal notranslate"><span class="pre">collectstatic</span></code> 时（如果 <code class="docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code> 不是空的），只有当文件的修改时间戳大于 <code class="docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code> 中文件的时间戳时，才会被复制。因此，如果你从 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 中删除一个应用程序，最好使用 <a class="reference internal" href="#cmdoption-collectstatic-clear"><code class="xref std std-option docutils literal notranslate"><span class="pre">collectstatic</span> <span class="pre">--clear</span></code></a> 选项来删除过时的静态文件。</p>
<p>通过使用 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_FINDERS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">启用的查找器</span></code></a> 搜索文件。默认情况是在 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_DIRS</span></code></a> 中定义的所有位置和 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置指定的应用程序的 <code class="docutils literal notranslate"><span class="pre">'static'</span></code> 目录中查找。</p>
<p><a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a> 管理命令在每次运行后都会调用 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 中的 <a class="reference internal" href="#django.contrib.staticfiles.storage.StaticFilesStorage.post_process" title="django.contrib.staticfiles.storage.StaticFilesStorage.post_process"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_process()</span></code></a> 方法，并传递一个管理命令找到的路径列表。它还接收 <a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a> 的所有命令行选项。这是由 <a class="reference internal" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage" title="django.contrib.staticfiles.storage.ManifestStaticFilesStorage"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code></a> 默认使用的。</p>
<p>默认情况下，收集的文件从 <a class="reference internal" href="../settings.html#std:setting-FILE_UPLOAD_PERMISSIONS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">FILE_UPLOAD_PERMISSIONS</span></code></a> 中获得权限，收集的目录从 <a class="reference internal" href="../settings.html#std:setting-FILE_UPLOAD_DIRECTORY_PERMISSIONS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">FILE_UPLOAD_DIRECTORY_PERMISSIONS</span></code></a> 中获得权限。如果你希望这些文件和／或目录有不同的权限，你可以将 <a class="reference internal" href="#staticfiles-storages"><span class="std std-ref">静态文件存储类</span></a> 中的任何一个子类化，并分别指定 <code class="docutils literal notranslate"><span class="pre">file_permissions_mode</span></code> 和／或 <code class="docutils literal notranslate"><span class="pre">directory_permissions_mode</span></code> 参数。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.staticfiles</span> <span class="kn">import</span> <span class="n">storage</span>

<span class="k">class</span> <span class="nc">MyStaticFilesStorage</span><span class="p">(</span><span class="n">storage</span><span class="o">.</span><span class="n">StaticFilesStorage</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;file_permissions_mode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mo">0o640</span>
        <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;directory_permissions_mode&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mo">0o760</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>然后将 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">'path.to.MyStaticFilesStorage'</span></code>。</p>
<p>一些常用的选项是：</p>
<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-noinput">
<code class="descname">--noinput</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">--no-input</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-collectstatic-noinput" title="永久链接至目标">¶</a></dt>
<dd><p>不要提示用户进行任何形式的输入。</p>
</dd></dl>

<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-ignore">
<code class="descname">--ignore</code><code class="descclassname"> PATTERN</code><code class="descclassname">, </code><code class="descname">-i</code><code class="descclassname"> PATTERN</code><a class="headerlink" href="#cmdoption-collectstatic-ignore" title="永久链接至目标">¶</a></dt>
<dd><p>忽略与此 glob 样式模式匹配的文件、目录或路径。多次使用可以忽略更多的文件、目录或路径。当指定路径时，始终使用正斜线，即使在 Windows 上也是如此。</p>
</dd></dl>

<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-dry-run">
<code class="descname">--dry-run</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">-n</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-collectstatic-dry-run" title="永久链接至目标">¶</a></dt>
<dd><p>除了修改文件系统外，其他都要做。</p>
</dd></dl>

<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-clear">
<code class="descname">--clear</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">-c</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-collectstatic-clear" title="永久链接至目标">¶</a></dt>
<dd><p>在尝试复制或链接原始文件之前，先清除现有文件。</p>
</dd></dl>

<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-link">
<code class="descname">--link</code><code class="descclassname"></code><code class="descclassname">, </code><code class="descname">-l</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-collectstatic-link" title="永久链接至目标">¶</a></dt>
<dd><p>为每个文件创建一个符号链接，而不是复制。</p>
</dd></dl>

<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-no-post-process">
<code class="descname">--no-post-process</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-collectstatic-no-post-process" title="永久链接至目标">¶</a></dt>
<dd><p>不要调用配置的 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 存储后端的 <a class="reference internal" href="#django.contrib.staticfiles.storage.StaticFilesStorage.post_process" title="django.contrib.staticfiles.storage.StaticFilesStorage.post_process"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_process()</span></code></a> 方法。</p>
</dd></dl>

<dl class="django-admin-option">
<dt id="cmdoption-collectstatic-no-default-ignore">
<code class="descname">--no-default-ignore</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-collectstatic-no-default-ignore" title="永久链接至目标">¶</a></dt>
<dd><p>不要忽视常见的私有 glob 样式模式 <code class="docutils literal notranslate"><span class="pre">'CVS'</span></code>、<code class="docutils literal notranslate"><span class="pre">'.*'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'*~'</span></code>。</p>
</dd></dl>

<p>完整的选项列表，请参考命令本身的帮助，运行：</p>
<div class="console-block" id="console-block-0">
<input class="c-tab-unix" id="c-tab-0-unix" type="radio" name="console-0" checked>
<label for="c-tab-0-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-0-win" type="radio" name="console-0">
<label for="c-tab-0-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-0-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python manage.py collectstatic --help
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-0-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py manage.py collectstatic --help
</pre></div>
</section>
</div>
<div class="section" id="s-customizing-the-ignored-pattern-list">
<span id="s-customize-staticfiles-ignore-patterns"></span><span id="customizing-the-ignored-pattern-list"></span><span id="customize-staticfiles-ignore-patterns"></span><h4>自定义忽略的模式列表<a class="headerlink" href="#customizing-the-ignored-pattern-list" title="永久链接至标题">¶</a></h4>
<p>默认的忽略模式列表 <code class="docutils literal notranslate"><span class="pre">['CVS',</span> <span class="pre">'.*',</span> <span class="pre">'*~']</span></code>，可以用比在每次 <code class="docutils literal notranslate"><span class="pre">collectstatic</span></code> 调用时提供 <code class="docutils literal notranslate"><span class="pre">--ignore</span></code> 命令选项更持久的方式进行自定义。提供一个自定义的 <a class="reference internal" href="../applications.html#django.apps.AppConfig" title="django.apps.AppConfig"><code class="xref py py-class docutils literal notranslate"><span class="pre">AppConfig</span></code></a> 类，覆盖这个类的 <code class="docutils literal notranslate"><span class="pre">ignore_patterns</span></code> 属性，并在你的 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 设置中用该类路径替换 <code class="docutils literal notranslate"><span class="pre">'django.contrib.staticfiles'</span></code>：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.staticfiles.apps</span> <span class="kn">import</span> <span class="n">StaticFilesConfig</span>

<span class="k">class</span> <span class="nc">MyStaticFilesConfig</span><span class="p">(</span><span class="n">StaticFilesConfig</span><span class="p">):</span>
    <span class="n">ignore_patterns</span> <span class="o">=</span> <span class="p">[</span><span class="o">...</span><span class="p">]</span>  <span class="c1"># your custom ignore list</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-findstatic">
<span id="findstatic"></span><h3><code class="docutils literal notranslate"><span class="pre">findstatic</span></code><a class="headerlink" href="#findstatic" title="永久链接至标题">¶</a></h3>
<dl class="django-admin">
<dt id="django-admin-findstatic">
<code class="descname">django-admin findstatic staticfile [staticfile ...]</code><a class="headerlink" href="#django-admin-findstatic" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>通过启用的查找器搜索一个或多个相对路径。</p>
<p>例如：</p>
<div class="console-block" id="console-block-1">
<input class="c-tab-unix" id="c-tab-1-unix" type="radio" name="console-1" checked>
<label for="c-tab-1-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-1-win" type="radio" name="console-1">
<label for="c-tab-1-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-1-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python manage.py findstatic css/base.css admin/js/core.js
<span class="go">Found &#39;css/base.css&#39; here:</span>
<span class="go">  /home/special.polls.com/core/static/css/base.css</span>
<span class="go">  /home/polls.com/core/static/css/base.css</span>
<span class="go">Found &#39;admin/js/core.js&#39; here:</span>
<span class="go">  /home/polls.com/src/django/contrib/admin/media/js/core.js</span>
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-1-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py manage.py findstatic css\base.css admin\js\core.js
<span class="go">Found &#39;css/base.css&#39; here:</span>
<span class="go">  /home/special.polls.com/core/static/css/base.css</span>
<span class="go">  /home/polls.com/core/static/css/base.css</span>
<span class="go">Found &#39;admin/js/core.js&#39; here:</span>
<span class="go">  /home/polls.com/src/django/contrib/admin/media/js/core.js</span>
</pre></div>
</section>
</div>
<dl class="django-admin-option">
<dt id="cmdoption-findstatic-arg-findstatic">
<code class="descname">findstatic</code><code class="descclassname"> --first</code><a class="headerlink" href="#cmdoption-findstatic-arg-findstatic" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>默认情况下，会找到所有匹配的位置。要只返回每个相对路径的第一个匹配点，请使用 <code class="docutils literal notranslate"><span class="pre">--first</span></code> 选项：</p>
<div class="console-block" id="console-block-2">
<input class="c-tab-unix" id="c-tab-2-unix" type="radio" name="console-2" checked>
<label for="c-tab-2-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-2-win" type="radio" name="console-2">
<label for="c-tab-2-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-2-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python manage.py findstatic css/base.css --first
<span class="go">Found &#39;css/base.css&#39; here:</span>
<span class="go">  /home/special.polls.com/core/static/css/base.css</span>
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-2-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py manage.py findstatic css\base.css --first
<span class="go">Found &#39;css/base.css&#39; here:</span>
<span class="go">  /home/special.polls.com/core/static/css/base.css</span>
</pre></div>
</section>
</div>
<p>这是一个调试辅助工具，它会告诉你到底哪个静态文件会被收集到一个给定的路径。</p>
<p>通过将 <code class="docutils literal notranslate"><span class="pre">--verbosity</span></code> 标志设置为 0，你可以抑制额外的输出，只获取路径名：</p>
<div class="console-block" id="console-block-3">
<input class="c-tab-unix" id="c-tab-3-unix" type="radio" name="console-3" checked>
<label for="c-tab-3-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-3-win" type="radio" name="console-3">
<label for="c-tab-3-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-3-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python manage.py findstatic css/base.css --verbosity <span class="m">0</span>
<span class="go">/home/special.polls.com/core/static/css/base.css</span>
<span class="go">/home/polls.com/core/static/css/base.css</span>
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-3-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py manage.py findstatic css\base.css --verbosity 0
<span class="go">/home/special.polls.com/core/static/css/base.css</span>
<span class="go">/home/polls.com/core/static/css/base.css</span>
</pre></div>
</section>
</div>
<p>另一方面，通过将 <code class="docutils literal notranslate"><span class="pre">--verbosity</span></code> 标志设置为 2，可以得到所有被搜索的目录。</p>
<div class="console-block" id="console-block-4">
<input class="c-tab-unix" id="c-tab-4-unix" type="radio" name="console-4" checked>
<label for="c-tab-4-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-4-win" type="radio" name="console-4">
<label for="c-tab-4-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-4-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python manage.py findstatic css/base.css --verbosity <span class="m">2</span>
<span class="go">Found &#39;css/base.css&#39; here:</span>
<span class="go">  /home/special.polls.com/core/static/css/base.css</span>
<span class="go">  /home/polls.com/core/static/css/base.css</span>
<span class="go">Looking in the following locations:</span>
<span class="go">  /home/special.polls.com/core/static</span>
<span class="go">  /home/polls.com/core/static</span>
<span class="go">  /some/other/path/static</span>
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-4-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> py manage.py findstatic css\base.css --verbosity 2
<span class="go">Found &#39;css/base.css&#39; here:</span>
<span class="go">  /home/special.polls.com/core/static/css/base.css</span>
<span class="go">  /home/polls.com/core/static/css/base.css</span>
<span class="go">Looking in the following locations:</span>
<span class="go">  /home/special.polls.com/core/static</span>
<span class="go">  /home/polls.com/core/static</span>
<span class="go">  /some/other/path/static</span>
</pre></div>
</section>
</div>
</div>
<div class="section" id="s-runserver">
<span id="s-staticfiles-runserver"></span><span id="runserver"></span><span id="staticfiles-runserver"></span><h3><code class="docutils literal notranslate"><span class="pre">runserver</span></code><a class="headerlink" href="#runserver" title="永久链接至标题">¶</a></h3>
<dl class="django-admin">
<dt>
<code class="descname">django-admin runserver [addrport]</code></dt>
<dd></dd></dl>

<p>如果 <code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 应用程序被 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">installed</span></code></a>，则覆盖核心 <a class="reference internal" href="../django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">runserver</span></code></a> 命令，并增加静态文件的自动服务。文件服务不通过 <a class="reference internal" href="../settings.html#std:setting-MIDDLEWARE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MIDDLEWARE</span></code></a> 运行。</p>
<p>该命令增加了这些选项：</p>
<dl class="django-admin-option">
<dt id="cmdoption-runserver-nostatic">
<code class="descname">--nostatic</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-runserver-nostatic" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>使用 <code class="docutils literal notranslate"><span class="pre">--nostatic</span></code> 选项来完全禁止使用 <a class="reference internal" href="#"><span class="doc">静态文件</span></a> 应用程序提供静态文件。只有当 <a class="reference internal" href="#"><span class="doc">静态文件</span></a> 应用在你的项目的 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中时，这个选项才可用。</p>
<p>使用实例：</p>
<div class="console-block" id="console-block-5">
<input class="c-tab-unix" id="c-tab-5-unix" type="radio" name="console-5" checked>
<label for="c-tab-5-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-5-win" type="radio" name="console-5">
<label for="c-tab-5-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-5-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>django-admin runserver --nostatic
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-5-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> django-admin runserver --nostatic
</pre></div>
</section>
</div>
<dl class="django-admin-option">
<dt id="cmdoption-runserver-insecure">
<code class="descname">--insecure</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-runserver-insecure" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>使用 <code class="docutils literal notranslate"><span class="pre">--insecure</span></code> 选项来强制使用 <a class="reference internal" href="#"><span class="doc">静态文件</span></a> 应用服务静态文件，即使 <a class="reference internal" href="../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。通过使用这个选项，你承认了这样的事实，即这是 <strong>效率低下的</strong>，而且可能是 <strong>不安全的</strong>。这仅用于本地开发，<strong>不应在生产中使用</strong>，并且只有当 <a class="reference internal" href="#"><span class="doc">静态文件</span></a> 应用在你的项目的 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 设置中时才可用。</p>
<p><code class="docutils literal notranslate"><span class="pre">--insecure</span></code> 不能与 <a class="reference internal" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage" title="django.contrib.staticfiles.storage.ManifestStaticFilesStorage"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code></a> 一起工作。</p>
<p>使用实例：</p>
<div class="console-block" id="console-block-6">
<input class="c-tab-unix" id="c-tab-6-unix" type="radio" name="console-6" checked>
<label for="c-tab-6-unix" title="Linux/macOS">&#xf17c/&#xf179</label>
<input class="c-tab-win" id="c-tab-6-win" type="radio" name="console-6">
<label for="c-tab-6-win" title="Windows">&#xf17a</label>
<section class="c-content-unix" id="c-content-6-unix">
<div class="highlight-console notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>django-admin runserver --insecure
</pre></div>
</div>
</section>
<section class="c-content-win" id="c-content-6-win">
<div class="highlight"><pre><span></span><span class="gp">...\&gt;</span> django-admin runserver --insecure
</pre></div>
</section>
</div>
</div>
</div>
<div class="section" id="s-storages">
<span id="s-staticfiles-storages"></span><span id="storages"></span><span id="staticfiles-storages"></span><h2>存储<a class="headerlink" href="#storages" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-staticfilesstorage">
<span id="staticfilesstorage"></span><h3><code class="docutils literal notranslate"><span class="pre">StaticFilesStorage</span></code><a class="headerlink" href="#staticfilesstorage" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.staticfiles.storage.StaticFilesStorage">
<em class="property">class </em><code class="descclassname">storage.</code><code class="descname">StaticFilesStorage</code><a class="headerlink" href="#django.contrib.staticfiles.storage.StaticFilesStorage" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p><a class="reference internal" href="../files/storage.html#django.core.files.storage.FileSystemStorage" title="django.core.files.storage.FileSystemStorage"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileSystemStorage</span></code></a> 存储后端的一个子类，分别使用 <a class="reference internal" href="../settings.html#std:setting-STATIC_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code></a> 配置作为基础文件系统位置和 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 配置作为基础 URL。</p>
<dl class="method">
<dt id="django.contrib.staticfiles.storage.StaticFilesStorage.post_process">
<code class="descclassname">storage.StaticFilesStorage.</code><code class="descname">post_process</code>(<em>paths</em>, <em>**options</em>)<a class="headerlink" href="#django.contrib.staticfiles.storage.StaticFilesStorage.post_process" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>如果这个方法定义在一个存储上，那么在每次运行后都会被 <a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a> 管理命令调用，并得到本地存储空间和找到的文件路径作为一个字典，以及命令行选项。它产生的元组有三个值。<code class="docutils literal notranslate"><span class="pre">original_path,</span> <span class="pre">processed_path,</span> <span class="pre">processed</span></code>。路径值是字符串，<code class="docutils literal notranslate"><span class="pre">processed</span></code> 是一个布尔值，表示该值是否经过后处理，如果后处理失败则表示异常。</p>
<p><a class="reference internal" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage" title="django.contrib.staticfiles.storage.ManifestStaticFilesStorage"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code></a> 在幕后使用它将路径替换为它们的哈希对应物，并适当地更新缓存。</p>
</div>
<div class="section" id="s-manifeststaticfilesstorage">
<span id="manifeststaticfilesstorage"></span><h3><code class="docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code><a class="headerlink" href="#manifeststaticfilesstorage" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.staticfiles.storage.ManifestStaticFilesStorage">
<em class="property">class </em><code class="descclassname">storage.</code><code class="descname">ManifestStaticFilesStorage</code><a class="headerlink" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p><a class="reference internal" href="#django.contrib.staticfiles.storage.StaticFilesStorage" title="django.contrib.staticfiles.storage.StaticFilesStorage"><code class="xref py py-class docutils literal notranslate"><span class="pre">StaticFilesStorage</span></code></a> 存储后台的一个子类，它通过在文件名上附加文件内容的 MD5 哈希值来存储它处理的文件名。例如，文件 <code class="docutils literal notranslate"><span class="pre">css/styles.css</span></code> 也会被保存为 <code class="docutils literal notranslate"><span class="pre">css/styles.55e7cbb9ba48.css</span></code>。</p>
<p>这个存储的目的是为了继续服务于旧文件，以防某些页面仍然引用这些文件，例如，因为它们被你或第三方代理服务器缓存了。此外，如果你想在部署的文件上应用 <a class="reference external" href="https://developer.yahoo.com/performance/rules.html#expires">远期失效头信息</a> ，以加快后续页面访问的加载时间，它是非常有用的。</p>
<p>存储后台会自动将保存文件中找到的与其他保存文件相匹配的路径替换为缓存副本的路径（使用 <a class="reference internal" href="#django.contrib.staticfiles.storage.StaticFilesStorage.post_process" title="django.contrib.staticfiles.storage.StaticFilesStorage.post_process"><code class="xref py py-meth docutils literal notranslate"><span class="pre">post_process()</span></code></a> 方法）。用于查找这些路径的正则表达式（<code class="docutils literal notranslate"><span class="pre">django.contrib.staticfiles.storage.HashedFilesMixin.patterns</span></code>）默认涵盖了 <a class="reference external" href="https://www.w3.org/TR/CSS2/cascade.html#at-import">&#64;import</a> 规则和 <a class="reference external" href="https://www.w3.org/TR/CSS2/syndata.html#uri">url()</a> 语句的 <a class="reference external" href="https://www.w3.org/Style/CSS/">层叠样式表</a> 。例如，<code class="docutils literal notranslate"><span class="pre">''css/styles.css'</span></code> 文件的内容为</p>
<div class="highlight-css notranslate"><div class="highlight"><pre><span></span><span class="p">@</span><span class="k">import</span> <span class="nt">url</span><span class="o">(</span><span class="s2">&quot;../admin/css/base.css&quot;</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</div>
<p>会通过调用 <code class="docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code> 存储后台的 <a class="reference internal" href="../files/storage.html#django.core.files.storage.Storage.url" title="django.core.files.storage.Storage.url"><code class="xref py py-meth docutils literal notranslate"><span class="pre">url()</span></code></a> 方法来替换，最终保存一个 <code class="docutils literal notranslate"><span class="pre">'css/styles.55e7cbb9ba48.css'</span></code> 文件，内容如下：</p>
<div class="highlight-css notranslate"><div class="highlight"><pre><span></span><span class="p">@</span><span class="k">import</span> <span class="nt">url</span><span class="o">(</span><span class="s2">&quot;../admin/css/base.27e20196a850.css&quot;</span><span class="o">)</span><span class="p">;</span>
</pre></div>
</div>
<dl class="attribute">
<dt id="django.contrib.staticfiles.storage.ManifestStaticFilesStorage.max_post_process_passes">
<code class="descclassname">storage.ManifestStaticFilesStorage.</code><code class="descname">max_post_process_passes</code><a class="headerlink" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.max_post_process_passes" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>由于静态文件可能会引用其他需要替换路径的静态文件，因此可能需要多次替换路径，直到文件哈希值收敛。为了防止由于哈希值不收敛而导致无限循环（例如，如果 <code class="docutils literal notranslate"><span class="pre">'foo.css'</span></code> 引用 <code class="docutils literal notranslate"><span class="pre">'bar.css'</span></code>，而后者引用 <code class="docutils literal notranslate"><span class="pre">'foo.css'</span></code>），在放弃后处理之前有一个最大的传递次数。在引用数量较多的情况下，可能需要更多的传递次数。通过子类 <code class="docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code> 并设置 <code class="docutils literal notranslate"><span class="pre">max_post_process_passes</span></code> 属性来增加最大传递次数。默认值为 5。</p>
<p>要启用 <code class="docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code>，你必须确保满足以下要求：</p>
<ul class="simple">
<li><a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'</span></code></li>
<li><a class="reference internal" href="../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code></li>
<li>通过使用 <a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a> 管理命令，你已经收集了所有静态文件</li>
</ul>
<p>由于在运行时创建 MD5 哈希可能会给网站带来性能负担，<code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 会自动将所有处理过的文件的哈希名映射存储在一个名为 <code class="docutils literal notranslate"><span class="pre">staticfiles.json</span></code> 的文件中。当你运行 <a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a> 管理命令时就会发生一次。</p>
<dl class="attribute">
<dt id="django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict">
<code class="descclassname">storage.ManifestStaticFilesStorage.</code><code class="descname">manifest_strict</code><a class="headerlink" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>如果在运行时在 <code class="docutils literal notranslate"><span class="pre">staticfiles.json</span></code> 清单中没有找到文件，就会引发 <code class="docutils literal notranslate"><span class="pre">ValueError</span></code>。这种行为可以通过子类 <code class="docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code> 并将 <code class="docutils literal notranslate"><span class="pre">manifest_strict</span></code> 属性设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 来禁止，不存在的路径将保持不变。</p>
<p>由于需要运行 <a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a>，在运行测试时通常不应该使用这个存储，因为 <code class="docutils literal notranslate"><span class="pre">collectstatic</span></code> 并不是作为正常测试配置的一部分来运行的。在测试过程中，确保将 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 设置为类似 <code class="docutils literal notranslate"><span class="pre">'django.contrib.staticfiles.storage.StaticFilesStorage'</span></code> （默认）这样的其他配置。</p>
<dl class="method">
<dt id="django.contrib.staticfiles.storage.ManifestStaticFilesStorage.file_hash">
<code class="descclassname">storage.ManifestStaticFilesStorage.</code><code class="descname">file_hash</code>(<em>name</em>, <em>content=None</em>)<a class="headerlink" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.file_hash" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>创建文件的哈希名时使用的方法。需要返回给定文件名和内容的哈希值。默认情况下，它从上面提到的内容块中计算出一个 MD5 哈希。可以随意覆盖这个方法，使用自己的哈希算法。</p>
</div>
<div class="section" id="s-manifestfilesmixin">
<span id="manifestfilesmixin"></span><h3><code class="docutils literal notranslate"><span class="pre">ManifestFilesMixin</span></code><a class="headerlink" href="#manifestfilesmixin" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.staticfiles.storage.ManifestFilesMixin">
<em class="property">class </em><code class="descclassname">storage.</code><code class="descname">ManifestFilesMixin</code><a class="headerlink" href="#django.contrib.staticfiles.storage.ManifestFilesMixin" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>使用这个混入和一个自定义的存储来附加文件内容的 MD5 哈希到文件名中，就像 <a class="reference internal" href="#django.contrib.staticfiles.storage.ManifestStaticFilesStorage" title="django.contrib.staticfiles.storage.ManifestStaticFilesStorage"><code class="xref py py-class docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code></a> 那样。</p>
</div>
</div>
<div class="section" id="s-finders-module">
<span id="finders-module"></span><h2>查找器模块<a class="headerlink" href="#finders-module" title="永久链接至标题">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 查找器有一个 <code class="docutils literal notranslate"><span class="pre">searched_locations</span></code> 属性，是查找器搜索的目录路径列表。使用示例：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.staticfiles</span> <span class="kn">import</span> <span class="n">finders</span>

<span class="n">result</span> <span class="o">=</span> <span class="n">finders</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="s1">&#39;css/base.css&#39;</span><span class="p">)</span>
<span class="n">searched_locations</span> <span class="o">=</span> <span class="n">finders</span><span class="o">.</span><span class="n">searched_locations</span>
</pre></div>
</div>
</div>
<div class="section" id="s-other-helpers">
<span id="other-helpers"></span><h2>其他辅助功能<a class="headerlink" href="#other-helpers" title="永久链接至标题">¶</a></h2>
<p>在 <a class="reference internal" href="#module-django.contrib.staticfiles" title="django.contrib.staticfiles: An app for handling static files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">staticfiles</span></code></a> 应用程序之外，还有一些其他的辅助功能来处理静态文件。</p>
<ul class="simple">
<li><a class="reference internal" href="../templates/api.html#django.template.context_processors.static" title="django.template.context_processors.static"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.template.context_processors.static()</span></code></a> 上下文处理器，它为每个用  <a class="reference internal" href="../templates/api.html#django.template.RequestContext" title="django.template.RequestContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">RequestContext</span></code></a> 上下文渲染的模板上下文添加 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a>。</li>
<li>内置的模板标签 <a class="reference internal" href="../templates/builtins.html#std:templatetag-static"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">static</span></code></a>，它接收一个路径，并用静态前缀 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 将其连接起来。如果安装了 <code class="docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code>，则该标签使用 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 的 <code class="docutils literal notranslate"><span class="pre">url()</span></code> 方法。</li>
<li>内置的模板标签 <a class="reference internal" href="../templates/builtins.html#std:templatetag-get_static_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a>，将静态前缀 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 填充到模板变量中，作为变量或直接使用。</li>
<li>类似的模板标签 <a class="reference internal" href="../templates/builtins.html#std:templatetag-get_media_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_media_prefix</span></code></a>，其工作原理与 <a class="reference internal" href="../templates/builtins.html#std:templatetag-get_static_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a> 类似，但使用 <a class="reference internal" href="../settings.html#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a>。</li>
</ul>
<div class="section" id="s-static-file-development-view">
<span id="s-staticfiles-development-view"></span><span id="static-file-development-view"></span><span id="staticfiles-development-view"></span><h3>静态文件开发视图<a class="headerlink" href="#static-file-development-view" title="永久链接至标题">¶</a></h3>
<p>静态文件工具主要是为了帮助将静态文件成功部署到生产中。这通常意味着一个单独的、专用的静态文件服务器，这在本地开发时是一个很大的开销。因此，<code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 应用程序中附带了一个 <strong>快速和肮脏的辅助视图</strong>，你可以在开发中使用它在本地服务文件。</p>
<dl class="function">
<dt id="django.contrib.staticfiles.views.serve">
<code class="descclassname">views.</code><code class="descname">serve</code>(<em>request</em>, <em>path</em>)<a class="headerlink" href="#django.contrib.staticfiles.views.serve" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>该视图功能服务于开发中的静态文件。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>只有当 <a class="reference internal" href="../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 时，该视图才会生效。</p>
<p class="last">这是因为这个视图的 <strong>效率很低</strong>，而且可能 <strong>不安全</strong>。这只用于本地开发，<strong>不应该用于生产</strong>。</p>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">为了猜测服务文件的内容类型，这个视图依赖于 Python 标准库中的 <a class="reference external" href="https://docs.python.org/3/library/mimetypes.html#module-mimetypes" title="(在 Python v3.10)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code></a> 模块，它本身依赖于底层平台的映射文件。如果你发现这个视图没有为某些文件返回正确的内容类型，很可能是平台的映射文件不正确或者需要更新。例如，可以通过安装或更新 Red Hat 发行版上的 <code class="docutils literal notranslate"><span class="pre">mailcap</span></code> 包、Debian 发行版上的 <code class="docutils literal notranslate"><span class="pre">mime-support</span></code>，或编辑 Windows 注册表中 <code class="docutils literal notranslate"><span class="pre">HKEY_CLASSES_ROOT</span></code> 下的键来实现。</p>
</div>
<p>该视图由 <a class="reference internal" href="../django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">runserver</span></code></a> 自动启用（将 <a class="reference internal" href="../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>）。要在不同的本地开发服务器上使用该视图，请在主 URL 配置的结尾添加以下代码段：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib.staticfiles</span> <span class="kn">import</span> <span class="n">views</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">re_path</span>

<span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
    <span class="n">urlpatterns</span> <span class="o">+=</span> <span class="p">[</span>
        <span class="n">re_path</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;^static/(?P&lt;path&gt;.*)$&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">serve</span><span class="p">),</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>注意，模式的开头（<code class="docutils literal notranslate"><span class="pre">r'^static/'</span></code>）应该是你的 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 配置。</p>
<p>因为这个有点细，所以还有一个辅助功能可以帮你完成这个任务：</p>
<dl class="function">
<dt id="django.contrib.staticfiles.urls.staticfiles_urlpatterns">
<code class="descclassname">urls.</code><code class="descname">staticfiles_urlpatterns</code>()<a class="headerlink" href="#django.contrib.staticfiles.urls.staticfiles_urlpatterns" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>这将返回适当的 URL 模式，用于向你已经定义的模式列表提供静态文件。像这样使用它：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.staticfiles.urls</span> <span class="kn">import</span> <span class="n">staticfiles_urlpatterns</span>

<span class="c1"># ... the rest of your URLconf here ...</span>

<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">staticfiles_urlpatterns</span><span class="p">()</span>
</pre></div>
</div>
<p>这将检查你的 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 配置，并相应地给视图设置静态文件。不要忘记设置 <a class="reference internal" href="../settings.html#std:setting-STATICFILES_DIRS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_DIRS</span></code></a> 配置，让 <code class="docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code> 知道除了应用程序目录中的文件外，还可以在哪里查找文件。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p>只有当 <a class="reference internal" href="../settings.html#std:setting-DEBUG"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DEBUG</span></code></a> 是 <code class="docutils literal notranslate"><span class="pre">True</span></code>，并且你的 <a class="reference internal" href="../settings.html#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 的配置既不是空的，也不是完整的 URL，比如 <code class="docutils literal notranslate"><span class="pre">http://static.example.com/</span></code>，这个辅助函数才会起作用。</p>
<p class="last">这是因为这个视图的 <strong>效率很低</strong>，而且可能 <strong>不安全</strong>。这只用于本地开发，<strong>不应该用于生产</strong>。</p>
</div>
</div>
<div class="section" id="s-specialized-test-case-to-support-live-testing">
<span id="specialized-test-case-to-support-live-testing"></span><h3>支持“实时测试”的专门测试案例<a class="headerlink" href="#specialized-test-case-to-support-live-testing" title="永久链接至标题">¶</a></h3>
<dl class="class">
<dt id="django.contrib.staticfiles.testing.StaticLiveServerTestCase">
<em class="property">class </em><code class="descclassname">testing.</code><code class="descname">StaticLiveServerTestCase</code><a class="headerlink" href="#django.contrib.staticfiles.testing.StaticLiveServerTestCase" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>这个 untest TestCase 子类扩展了 <a class="reference internal" href="../../topics/testing/tools.html#django.test.LiveServerTestCase" title="django.test.LiveServerTestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.test.LiveServerTestCase</span></code></a>。</p>
<p>就像它的父类一样，你可以用它来编写测试，涉及到运行被测代码，并通过 HTTP 与测试工具（如 Selenium、PhantomJS 等）进行消费，因为这需要静态资产也被发布。</p>
<p>但是由于它使用了上面描述的 <code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.staticfiles.views.service()</span></code> 视图，它可以在测试执行时透明地叠加 <code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 查找器提供的资产。这意味着你不需要在测试配置之前或作为测试配置的一部分运行 <a class="reference internal" href="#django-admin-collectstatic"><code class="xref std std-djadmin docutils literal notranslate"><span class="pre">collectstatic</span></code></a>。</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="docutils literal notranslate"><span class="pre">staticfiles</span></code> 应用</a><ul>
<li><a class="reference internal" href="#settings">配置</a></li>
<li><a class="reference internal" href="#management-commands">管理命令</a><ul>
<li><a class="reference internal" href="#collectstatic"><code class="docutils literal notranslate"><span class="pre">collectstatic</span></code></a><ul>
<li><a class="reference internal" href="#customizing-the-ignored-pattern-list">自定义忽略的模式列表</a></li>
</ul>
</li>
<li><a class="reference internal" href="#findstatic"><code class="docutils literal notranslate"><span class="pre">findstatic</span></code></a></li>
<li><a class="reference internal" href="#runserver"><code class="docutils literal notranslate"><span class="pre">runserver</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#storages">存储</a><ul>
<li><a class="reference internal" href="#staticfilesstorage"><code class="docutils literal notranslate"><span class="pre">StaticFilesStorage</span></code></a></li>
<li><a class="reference internal" href="#manifeststaticfilesstorage"><code class="docutils literal notranslate"><span class="pre">ManifestStaticFilesStorage</span></code></a></li>
<li><a class="reference internal" href="#manifestfilesmixin"><code class="docutils literal notranslate"><span class="pre">ManifestFilesMixin</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#finders-module">查找器模块</a></li>
<li><a class="reference internal" href="#other-helpers">其他辅助功能</a><ul>
<li><a class="reference internal" href="#static-file-development-view">静态文件开发视图</a></li>
<li><a class="reference internal" href="#specialized-test-case-to-support-live-testing">支持“实时测试”的专门测试案例</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="sites.html"
                        title="上一章">“站点”框架</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="syndication.html"
                        title="下一章">Feed 聚合框架</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/contrib/staticfiles.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="sites.html" title="“站点”框架">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="syndication.html" title="Feed 聚合框架">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>